<template>
  <div class="Banner">
    <div class="header">
      <el-button type="primary" @click="showAddBanner">新增banner</el-button>
      <el-button type="primary" @click="isShowPreview = true">查看预览</el-button>
    </div>
    <el-table :data="list">
      <el-table-column prop="date" label="banner">
        <template slot-scope="scope">
          <span class="imgs">
            <img :src="scope.row.cover" alt />
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="主标题">
        <template slot-scope="scope">
          <span :style="{color:scope.row.color}">{{scope.row.title}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="title" label="副标题">
        <template slot-scope="scope">
          <span :style="{color:scope.row.color2}">{{scope.row.title2}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="title2" label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit( scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="showDeleteDialog( scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      title="预览"
      top="10vh"
      width="600px;"
      :close-on-click-modal="false"
      :visible.sync="isShowPreview"
    >
      <div class="banner-box" v-if="list.length">
        <el-carousel height="280px" indicator-position="outside" :interval="10000">
          <el-carousel-item v-for="item in list" :key="item.did">
            <div class="banner-img">
              <img :src="item.cover" alt />
              <div class="titles">
                <div class="zhu-title" :style="{color:item.color}">{{item.title}}</div>
                <div class="fu-title" :style="{color:item.color2}">{{item.title2}}</div>
              </div>
            </div>
          </el-carousel-item>
        </el-carousel>
      </div>
    </el-dialog>

    <el-dialog
      :title="dialogTitle"
      top="10vh"
      :close-on-click-modal="false"
      :visible.sync="isShowDialog"
    >
      <div class="each cover">
        <b>*</b>
        <span>banner:</span>
        <img :src="obj.cover" alt="banner" v-if="obj.cover" />
        <Upload :name="obj.cover?'重新上传':'上传图片'" type="customer" @saveCb="onUploadSuccess"></Upload>
      </div>
      <div class="each">
        <b class="b1">*</b>
        <span>主标题:</span>
        <input type="text" v-model="obj.title" :style="{color:obj.color}" placeholder="请输入主标题" />
        <div class="block">
          <span class="demonstration">主标题颜色</span>
          <el-color-picker v-model="obj.color"></el-color-picker>
        </div>
      </div>
      <div class="each">
        <b class="b1">*</b>
        <span>副标题:</span>
        <input type="text" v-model="obj.title2" :style="{color:obj.color2}" placeholder="请输入副标题" />
        <div class="block">
          <span class="demonstration">副标题颜色</span>
          <el-color-picker v-model="obj.color2"></el-color-picker>
        </div>
      </div>
      <div class="btns">
        <div class="btn">
          <el-button type="info" @click="isShowDialog = false">取消</el-button>
        </div>
        <div class="btn">
          <el-button type="primary" @click="save">确定</el-button>
        </div>
      </div>
    </el-dialog>
    <MyDialog
      title="删除"
      ref="deleteBanner"
      lastText="删除"
      :colorType="true"
      @saveHandle="deleteBanner"
    >
      <p class="tips">确定删除吗？</p>
      <p>删除后此banner前台将不再显示</p>
    </MyDialog>
  </div>
</template>
<script>
import Upload from "../components/upload";
import MyDialog from "../components/dialog";
export default {
  name: "Banner",
  components: { Upload, MyDialog },
  data() {
    return {
      list: [],
      did: "",
      color1: "#409EFF",
      isShowDialog: false,
      isShowPreview: false,
      obj: {
        cover: "",
        title: "",
        title2: "",
        color: "",
        color2: ""
      },
      dialogTitle: "新增banner",
      saveType: "add"
    };
  },
  methods: {
    //上传成功
    onUploadSuccess(val, uuid) {
      this.obj.cover = val;
    },

    //显示编辑
    handleEdit(row) {
      this.dialogTitle = "编辑banner";
      this.isShowDialog = true;
      this.saveType = "edit";
      this.obj = {
        cover: row.cover,
        title: row.title,
        title2: row.title2,
        color: row.color,
        color2: row.color2
      };
      this.did = row.did;
    },
    //显示新增banner
    showAddBanner() {
      this.obj = {
        cover: "",
        title: "",
        title2: "",
        color: "",
        color2: ""
      };
      this.dialogTitle = "新增banner";
      this.isShowDialog = true;
      this.saveType = "add";
    },

    //保存
    async save() {
      if (!this.obj.cover) {
        this.$message.error("请上传banner");
        return;
      }
      if (this.saveType == "add") {
        const res = await this.$post("/carousel/create", {
          cover: this.obj.cover,
          title: this.obj.title,
          title2: this.obj.title2,
          color: this.obj.color,
          color2: this.obj.color2
        });
        if (res.code == 200) {
          this.$message.success("上传成功");
          this.isShowDialog = false;
          this.getList();
        }
      } else {
        const res = await this.$post("/carousel/update", {
          did: this.did,
          cover: this.obj.cover,
          title: this.obj.title,
          title2: this.obj.title2,
          color: this.obj.color,
          color2: this.obj.color2
        });
        if (res.code == 200) {
          this.$message.success("编辑成功");
          this.isShowDialog = false;
          this.getList();
        }
      }
    },
    async uploadBanner(cover, uuid) {
      const res = await this.$post("/carousel/create", {
        cover,
        title: uuid
      });
      if (res.code == 200) {
        this.$message.success("上传成功");
        this.getList();
      }
    },
    async getList() {
      const res = await this.$post("carousel/list");
      if (res.code == 200) {
        const { data } = res;
        this.list = data.carousels;
      }
    },
    showDeleteDialog(row) {
      this.did = row.did;
      this.$refs.deleteBanner.show();
    },
    async deleteBanner() {
      const res = await this.$post("/carousel/delete", { did: this.did });
      if (res.code == 200) {
        this.$message.success("删除成功");
        this.$refs.deleteBanner.hide();
        this.getList();
      }
    }
  },
  created() {
    this.getList();
  },
  mounted() {
    this.$parent.setPannelName([
      {
        name: "banner管理",
        path: "/Banner",
        des: "本页面用于前端网站的首页的banner管理"
      }
    ]);
  }
};
</script>
<style lang="less" scoped>
.header {
  text-align: right;
  padding: 30px;
  border-bottom: 1px solid #ccc;
  display: flex;
}
.banner-img {
  outline: 1px solid red;
  margin-right: 30px;
  width: 100%;
  height: 280px;
  position: relative;
  img {
    width: 100%;
    height: 100%;
  }
}
.titles {
  position: absolute;
  width: 100%;
  height: 100%;
  z-index: 11;
  top: 0;
  left: 0;
  padding-top: 100px;
  box-sizing: border-box;
  text-align: center;
  .zhu-title {
    margin-bottom: 10px;
    font-size: 16px;
    font-weight: 600;
  }
  .fu-title {
    font-size: 12px;
  }
}
// .zhu-title {
//   position: absolute;
//   font-size: 36px;
//   width: 100%;
//   text-align: center;
//   top: 50px;
// }
// .fu-title {
//   position: absolute;
//   font-size: 24px;
//   width: 100%;
//   text-align: center;
//   top: 90px;
// }
.tips {
  font-weight: normal;
  font-size: 32px;
  margin-bottom: 50px;
}
.each {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  b {
    color: #fe6972;
  }
  > span {
    display: inline-block;
    width: 60px;
    margin-right: 20px;
  }
  > img {
    width: 105px;
    height: 120px;
    margin-right: 20px;
  }
  input {
    height: 40px;
    display: inline-block;
    width: 300px;
    border: 1px solid #409eff;
    outline: none;
    padding-left: 20px;
    border-radius: 4px;
  }
  .block {
    display: flex;
    align-items: center;
    margin-left: 20px;
  }
}
.each1 {
  b {
    color: #fe6972;
  }
}
.b1 {
  visibility: hidden;
}
.btns {
  display: flex;
  margin-top: 20px;
  justify-content: center;
  .btn {
    margin-right: 20px;
  }
}
.imgs {
  img {
    width: 200px;
    height: auto;
  }
}
</style>
